/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package assignment_cp3120.startup;

import assignment_cp3120.login.LoginView;
import assignment_cp3120.user.registeruser.RegisterUserView;
import assignment_cp3120.view.MainFrame;
import assignment_cp3120.viewtickets.ViewTicketsView;
import assignment_cp3120.purchasetickets.PurchaseTicketsView;
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;

/**
 * Startup view. This is the main view used by the program
 * @author Naomi
 */
public class StartupView extends MainFrame{
    private static final int BUTTON_WIDTH = 150;
    private static final int BUTTON_HEIGHT = 50;
    private static final int LAYOUT_HGAP = 20;
    private static final int LAYOUT_VGAP = 30;
    
    private JPanel buttonPanel;
    private JPanel purchasePanel;
    private JPanel loginPanel;
    private JPanel registerPanel;
    private JPanel viewFlightsPanel;
    
    private JTextArea purchaseInstructions;
    private JTextArea loginInstructions;
    private JTextArea registerInstructions;
    private JTextArea viewFlightsInstructions;
    
    private JButton purchaseTickets;
    private JButton login;
    private JButton register;
    private JButton viewTickets;
    
    private StartupModel model;
    private static final Dimension buttonSize = new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT);
    
    private static StartupView instance;
    /**
     * Default constructor calls Constructor(model) with a new model
     */
    protected StartupView(){
        this(new StartupModel());
    }
    /**
     * Constructor that sets the model to the input model and calls create frame
     * @param model
     */
    protected StartupView(StartupModel model){
        this.model = model;
        createFrame();
        instance = this;
    }
    public static StartupView getInstance(){
        if(instance == null){
            instance = new StartupView();
        }
        return instance;
    }
    /**
     * Creates the data to be added to the contents panel
     * @return
     * Returns the created panel
     */
    public JPanel createFrame() {
        model = new StartupModel();
        //Create the buttons        
        buttonPanel = new JPanel();                             //Create a panel to hold the buttons
        GridLayout buttonLayout = new GridLayout(0,1);          //Create a new grid layout
        buttonPanel.setLayout(buttonLayout);                    //Set the button panel to grid layout
        
        FlowLayout fl = new FlowLayout();
        fl.setHgap(LAYOUT_HGAP);                                         //Set the horizontal spacing between elements
        fl.setVgap(LAYOUT_VGAP);                                         //Set the vertical spacing between elements
        
        
        purchasePanel = new JPanel(fl);
        loginPanel = new JPanel(fl);
        registerPanel = new JPanel(fl);
        viewFlightsPanel = new JPanel(fl);
        
        
        purchaseTickets = new JButton("Purchase Tickets");          //Create the "Purchase Tickets" button
        purchaseTickets.setAlignmentX(Component.CENTER_ALIGNMENT);  //Centre the button
        purchaseTickets.setPreferredSize(buttonSize);        
   
        login = new JButton("Login");                               //Create the "Login" button
        login.setAlignmentX(Component.CENTER_ALIGNMENT);            //Centre the button
        login.setPreferredSize(buttonSize);
        
        register = new JButton("Register");
        register.setAlignmentX(Component.CENTER_ALIGNMENT);
        register.setPreferredSize(buttonSize);
        
        viewTickets = new JButton("View Flights");
        viewTickets.setAlignmentX(Component.CENTER_ALIGNMENT);
        viewTickets.setPreferredSize(buttonSize);
        
        //Display the instructions to purchase a ticket
        purchaseInstructions = new JTextArea("Click here to purchase flight tickets");    //Display the instructions to login to the system
        purchaseInstructions.setLineWrap(true);                        //Set line wrapping
        purchaseInstructions.setWrapStyleWord(true);                   //Set word wrapping
        purchaseInstructions.setEditable(false);                       //Make it uneditable
        purchaseInstructions.setBackground(null);                      //Make it transparent
        purchaseInstructions.setFont(new Font("sans serif", Font.BOLD, 12));   //Make the font bold
        purchaseInstructions.setAlignmentY(Component.CENTER_ALIGNMENT);        
        purchaseInstructions.setSize(buttonSize);
        
        loginInstructions = new JTextArea("Click here to login to the system and manage your account");    //Display the instructions to login to the system
        loginInstructions.setLineWrap(true);                        //Set line wrapping
        loginInstructions.setWrapStyleWord(true);                   //Set word wrapping
        loginInstructions.setEditable(false);                       //Make it uneditable
        loginInstructions.setBackground(null);                      //Make it transparent
        loginInstructions.setFont(new Font("sans serif", Font.BOLD, 12));   //Make the font bold
        loginInstructions.setAlignmentY(Component.CENTER_ALIGNMENT);
        loginInstructions.setSize(buttonSize);
        
        registerInstructions = new JTextArea("Click here to register a new account");    //Display the instructions to login to the system
        registerInstructions.setLineWrap(true);                        //Set line wrapping
        registerInstructions.setWrapStyleWord(true);                   //Set word wrapping
        registerInstructions.setEditable(false);                       //Make it uneditable
        registerInstructions.setBackground(null);                      //Make it transparent
        registerInstructions.setFont(new Font("sans serif", Font.BOLD, 12));   //Make the font bold
        registerInstructions.setAlignmentY(Component.CENTER_ALIGNMENT);
        registerInstructions.setSize(buttonSize);
        
        viewFlightsInstructions = new JTextArea("Click here to view avalible flights");    //Display the instructions to login to the system
        viewFlightsInstructions.setLineWrap(true);                        //Set line wrapping
        viewFlightsInstructions.setWrapStyleWord(true);                   //Set word wrapping
        viewFlightsInstructions.setEditable(false);                       //Make it uneditable
        viewFlightsInstructions.setBackground(null);                      //Make it transparent
        viewFlightsInstructions.setFont(new Font("sans serif", Font.BOLD, 12));   //Make the font bold
        viewFlightsInstructions.setAlignmentY(Component.CENTER_ALIGNMENT);
        viewFlightsInstructions.setSize(buttonSize);
        
        purchasePanel.add(purchaseTickets);                             //Add the button to the panel
        purchasePanel.add(purchaseInstructions);                        //Add the ticket purchasing instructions to the panel
        loginPanel.add(login);                                          //Add the button to the panel
        loginPanel.add(loginInstructions);                              //Add the login instructions to the panel
        
        registerPanel.add(register);                                    //Add button to the panel
        registerPanel.add(registerInstructions);                        //Add the register instuctions to the panel
        
        viewFlightsPanel.add(viewTickets);
        viewFlightsPanel.add(viewFlightsInstructions);
        
        buttonPanel.add(purchasePanel);                                 //
        buttonPanel.add(loginPanel);
        buttonPanel.add(registerPanel);
        buttonPanel.add(viewFlightsPanel);

        
        purchaseTickets.addActionListener(new ShowPurchaseTicketsAL());           //Set the action for the "Purchase Tickets" button
        
        login.addActionListener(new ShowLoginViewAL());                     //Set the action for the "Login" button
        
        register.addActionListener(new ShowRegisterViewAL());
        
        viewTickets.addActionListener(new ShowViewFlightsAL());
        
        //Add components to the window
        //Add the panel to the top of the window
        
        setContents(buttonPanel);           //Add the panel to the middle of the window
        return buttonPanel;
    }
    /**
     * set button listeners and action commands for all of the buttons
     * @param a
     * Action listener to be assigned to the buttons
     */
    private class ShowPurchaseTicketsAL implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            PurchaseTicketsView view = new PurchaseTicketsView();
            view.showGUI();
        }       
    }
    private class ShowLoginViewAL implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            LoginView.getInstance().showGUI();
        }       
    }
    private class ShowRegisterViewAL implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            RegisterUserView view = new RegisterUserView();
            view.showGUI();
        }       
    }
    private class ShowViewFlightsAL implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            ViewTicketsView view = new ViewTicketsView();
            setContents(view.createView());
        }       
    }
}
